<!DOCTYPE html>
<html>

<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <meta name="viewport"
    content="width=device-width,initial-scale=1,user-scalable=0,minimum-scale=1.0,maximum-scale=1.0" />
  <meta name="author" content="火星科技 http://mars3d.cn " />
  <meta name="apple-touch-fullscreen" content="yes" />
  <meta name="apple-mobile-web-app-capable" content="yes" />
  <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  <meta name="format-detection" content="telephone=no" />
  <meta name="x5-fullscreen" content="true" />
  <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1" />
  <!-- 标题及搜索关键字 -->
  <meta name="keywords" content="火星科技,cesium,3D,GIS,marsgis,三维,地球,地图,开发,框架,系统,示例,资料,模型,离线,外包,合肥,安徽,中国" />
  <meta name="description"
    content="火星科技 合肥火星 合肥火星科技 合肥火星科技有限公司 leaflet leaflet框架 leaflet开发 cesium cesium开发 cesium框架 三维 地球 模型  gis marsgis 地图离线 地图开发 地图框架 地图外包 框架 开发 外包  地图离线 二维地图 三维地图 全景漫游 地理信息系统 云GIS 三维GIS GIS平台 WebGIS" />

  <link rel="shortcut icon" type="image/x-icon" href="/img/favicon/favicon.ico" />
  <title>gltf零部件控制(运载火箭) | Mars3D | 三维地图 | 火星科技 | 合肥火星科技有限公司</title>

  <!--第三方lib-->
  <script type="text/javascript" src="/lib/include-lib.js" libpath="/lib/"
    include="jquery,layer,toastr,jquery.range,font-awesome,bootstrap,layer,haoutil,mars3d"></script>

  <link href="/css/style.css" rel="stylesheet" />
  <style>
    .infoview td {
      padding-left: 5px;
      padding-top: 5px;
    }
  </style>
</head>

<body class="dark">
  <div id="mars3dContainer" class="mars3d-container"></div>

  <div id="toolbar" class="infoview">
    <div>
      连接部件:
      <select class="cesium-button"
        data-bind="options: articulations,optionsText: 'name_cn',value: selectedArticulation"></select>
    </div>
    <table>
      <tbody data-bind="foreach: stages">
        <tr>
          <td data-bind="text: name_cn"></td>
          <td>
            <input type="range" min="-3" max="3" step="0.01"
              data-bind="value: current, valueUpdate: 'input',attr: {min: minimum,max: maximum}" />
          </td>
          <td>
            <input type="text" size="2" data-bind="value: current" />
          </td>
        </tr>
      </tbody>
    </table>
  </div>

  <script src="/js/common.js"></script>
  <script src="./map.js"></script>
  <script type="text/javascript">
    "use script" //开发环境建议开启严格模式

    //汉化属性名称
    let namesCN = {
      //属性分组
      LaunchVehicle: "火箭整体",
      Fairing: "整流罩",

      UpperStage: "二级部分",
      UpperStageEngines: "二级发动机",
      UpperStageFlames: "二级火焰",

      InterstageAdapter: "级间段",

      Booster: "一级部分",
      BoosterEngines: "一级发动机",
      BoosterFlames: "一级火焰",

      SRBs: "助推器",
      SRBFlames: "助推器火焰",

      //属性
      MoveX: "X方向移动",
      MoveY: "Y方向移动",
      MoveZ: "Z方向移动",
      Yaw: "Yaw角度",
      Pitch: "Pitch角度",
      Roll: "Roll角度",
      Size: "大小",
      Separate: "分离",
      Drop: "下降",
      Open: "打开",
      Rotate: "旋转"
    }

    function initUI(options) {
      //绑定到UI界面控制参数
      let viewModel = {
        articulations: [],
        stages: [],
        selectedArticulation: undefined
      }

      Cesium.knockout.track(viewModel)
      Cesium.knockout.getObservable(viewModel, "selectedArticulation").subscribe(function (newArticulation) {
        viewModel.stages = newArticulation.stages
      })

      let toolbar = document.getElementById("toolbar")
      Cesium.knockout.applyBindings(viewModel, toolbar)

      graphic.on(mars3d.EventType.load, (event) => {
        const model = event.model

        // 缩放区域到模型所在位置
        const controller = map.scene.screenSpaceCameraController
        const r = 2.0 * Math.max(model.boundingSphere.radius, map.camera.frustum.near)
        controller.minimumZoomDistance = r * 0.2

        const center = Cesium.Matrix4.multiplyByPoint(model.modelMatrix, Cesium.Cartesian3.ZERO, new Cesium.Cartesian3())
        const heading = Cesium.Math.toRadians(0.0)
        const pitch = Cesium.Math.toRadians(-10.0)
        map.camera.lookAt(center, new Cesium.HeadingPitchRange(heading, pitch, r * 1.5))

        // 设置参数效果
        model.setArticulationStage("LaunchVehicle Pitch", -60) // 火箭整体Pitch角度
        model.setArticulationStage("SRBFlames Size", 1) // 助推器火焰大小
        model.applyArticulations()

        // 读取gltf内的构件信息 cesium 1.97之前版本
        // const articulationsByName = model._runtime.articulationsByName
        // const articulations = Object.keys(articulationsByName).map(function (articulationName) {
        //   return {
        //     name: articulationName,
        //     name_cn: namesCN[articulationName] || articulationName, // 汉化
        //     stages: articulationsByName[articulationName].stages.map(function (stage) {
        //       const stageModel = {
        //         name: stage.name,
        //         name_cn: namesCN[stage.name] || stage.name, // 汉化
        //         minimum: stage.minimumValue,
        //         maximum: stage.maximumValue,
        //         current: stage.currentValue
        //       }
        //       return stageModel
        //     })
        //   }
        // })

        // 读取gltf内的构件信息  cesium 1.97+之后版本
        const articulationsByName = model.sceneGraph._runtimeArticulations
        viewModel.articulations = Object.keys(articulationsByName).map(function (articulationName) {
          return {
            name: articulationName,
            name_cn: namesCN[articulationName] || articulationName, // 汉化
            stages: articulationsByName[articulationName]._runtimeStages.map(function (stage) {
              const stageModel = {
                name: stage.name,
                name_cn: namesCN[stage.name] || stage.name, // 汉化
                minimum: stage.minimumValue,
                maximum: stage.maximumValue,
                current: stage.currentValue
              }
              // 事件
              Cesium.knockout.track(stageModel)
              Cesium.knockout.defineProperty(stageModel, "currentText", {
                get: function () {
                  return stageModel.current.toString()
                },
                set: function (value) {
                  // coerce values to number
                  stageModel.current = +value
                }
              })
              Cesium.knockout.getObservable(stageModel, "current").subscribe(function (newValue) {
                let _name = articulationName + " " + stage.name
                let _val = Number(stageModel.current)

                model.setArticulationStage(_name, _val)
                model.applyArticulations()
              })
              return stageModel
            })
          }
        })
      })
    }
  </script>
</body>

</html>